<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<?php
include ('general.php');

session_start();
if ($_POST['post']) {
    $usuario = mysql_real_escape_string( $_POST['usuario'] );
    $clave = mysql_real_escape_string( $_POST['clave'] );
    $clave = md5($clave);
    $passDB = executeScalar("Select Password from Usuarios where
            cod='$usuario' and Password='$clave'", null);
    if ($_GET['url'])
        $sec_page = $_GET['url'];
    else
        $sec_page = 'index.php';

    if ($passDB != null) {
        $_SESSION['username'] = $usuario;
        header("Location: $sec_page");
        exit;
    } else {
        echo "Wrong username or password";
    }
    //function md5 is useful for password hashing
}
if ($_POST['sendNewUser']) {
    $codigo = $_POST['codigo'];
    $nombre = $_POST['nombre'];
    $email= $_POST['email'];
    $password1 = $_POST['passwd1'];
    $password2 = $_POST['passwd2'];

    if (strcmp($password1, $password2) != 0) {
        exit;
    }
    $password = md5($password1);
    $sql = "insert into Usuarios (cod,Nombre,Password,Email) values ('$codigo','$nombre','$password','$email')";
    executeNonQuery($sql);
    $_SESSION['username'] = $codigo;
    header('Location: index.php');
    exit;
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
        <link href="resources/vividorange/style.css" rel="stylesheet" type="text/css" media="screen" />
    </head>
    <body>

        <div id="wrapper">

            <div id="header">
                <div id="logo">
                <h1>Estructura de datos</h1>
                Sebastián Gómez González - Universidad Tecnologica de Pereira
                </div>
            </div>
            <div id="page">
            <div id="page-bgtop">
            <div id="page-bgbtm">

                <div id="content">
                    <div class="post" id="mylogin">
                        <h2 class="title">Formulario de autenticación</h2>
                        <div style="clear: both;">&nbsp;</div>
                        <div class="entry">
                        <form id="mylogin" action="login.php" method="post">
                            <table class="tabla">
                                <tr>
                                    <td class="form_lbl"> Usuario: </td>
                                    <td> <input type="text" name="usuario" /> </td>
                                </tr>
                                <tr>
                                    <td class="form_lbl"> Password: </td>
                                    <td> <input type="password" name="clave" /> </td>
                                </tr>
                            </table>
                            <input type="submit" class="boton" name="post" value="Enviar" />
                        </form>
                        </div>
                    </div>

                    <div class="post" id="newuser">
                        <h2 class="title">Crear nuevo usuario</h2>
                        <div style="clear: both;">&nbsp;</div>
                        <div class="entry">
                            <form name="newuser" method="POST" action="newUser.php" onsubmit="return userSubmit();">
                                <table>
                                    <tr>
                                        <td>C&oacute;digo:</td>
                                        <td><input type="text" id="codigo" name="codigo" /></td>
                                    </tr>
                                    <tr>
                                        <td>Nombre completo:</td>
                                        <td><input type="text" id="nombre" name="nombre" /></td>
                                    </tr>
                                    <tr>
                                        <td>Email:</td>
                                        <td><input type="text" id="email" name="email" /></td>
                                    </tr>
                                    <tr>
                                        <td>Password:</td>
                                        <td><input type="password" id="passwd1" name="passwd1" /></td>
                                    </tr>
                                    <tr>
                                        <td>Confirmar password:</td>
                                        <td><input type="password" id="passwd2" name="passwd2" /></td>
                                    </tr>
                                </table>
                                <input type="submit" name="sendNewUser" value="Enviar" />
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            </div>
            </div>
        </div>

    <script type="text/javascript" src="resources/jquery.js"></script>
    <script type="text/javascript" src="resources/jquery.form.js"></script>

    <script language="javascript" >
        function userSubmit() {
            var passwd1 = $('#passwd1').val();
            var passwd2 = $('#passwd2').val();
            if (passwd1 != null && passwd1.length >= 5 && passwd1==passwd2)
                return true;
            alert('Las contraseñas no son iguales o son de menos de 5 caracteres');
            return false;
        }
    </script>

    </body>
</html>
